package org.ohos.rajawali3d.examples.examples.animation;

import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.utils.TextAlignment;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.ResourceManager;
import org.ohos.rajawali3d.ResourceTable;
import org.ohos.rajawali3d.examples.examples.AExampleFragment;
import org.rajawali3d.animation.mesh.VertexAnimationObject3D;
import org.rajawali3d.lights.DirectionalLight;
import org.rajawali3d.loader.LoaderMD2;
import org.rajawali3d.loader.ParsingException;

public class MD2Fragment extends AExampleFragment {
    private static final int BUTTON_WIDTH = 200;
    private static final int BUTTON_MARGIN_LEFT = 13;

    @Override
    public Component onCreateView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        DirectionalLayout mDirectionalLayout = new DirectionalLayout(getContext());
        ComponentContainer.LayoutConfig lc = mDirectionalLayout.getLayoutConfig();
        lc.width = DirectionalLayout.LayoutConfig.MATCH_PARENT;
        lc.height = DirectionalLayout.LayoutConfig.MATCH_PARENT;
        mDirectionalLayout.setOrientation(DirectionalLayout.HORIZONTAL);
        mDirectionalLayout.setAlignment(LayoutAlignment.TOP);

        Button button = new Button(getContext());
        button.setId(ResourceTable.Integer_md2_fragment_button_salute);
        button.setClickedListener(this);
        button.setText(ResourceTable.String_md2_fragment_button_salute);
        button.setTextSize(50);
        button.setTextAlignment(TextAlignment.HORIZONTAL_CENTER);
        button.setHeight(80);
        button.setTextColor(Color.BLACK);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(RgbColor.fromArgbInt(0xFFADADAD));
        button.setBackground(shapeElement);
        button.setClickable(true);
        button.setMarginLeft(BUTTON_MARGIN_LEFT);
        button.setWidth(BUTTON_WIDTH);

        mDirectionalLayout.addComponent(button);

        button = new Button(getContext());
        button.setId(ResourceTable.Integer_md2_fragment_button_death);
        button.setClickedListener(this);
        button.setText(ResourceTable.String_md2_fragment_button_death);
        button.setTextSize(50);
        button.setTextAlignment(TextAlignment.HORIZONTAL_CENTER);
        button.setHeight(80);
        button.setTextColor(Color.BLACK);
        shapeElement.setRgbColor(RgbColor.fromArgbInt(0xFFADADAD));
        button.setBackground(shapeElement);
        button.setClickable(true);
        button.setWidth(BUTTON_WIDTH);
        button.setMarginLeft(BUTTON_MARGIN_LEFT);
        mDirectionalLayout.addComponent(button);

        button = new Button(getContext());
        button.setId(ResourceTable.Integer_md2_fragment_button_walk);
        button.setClickedListener(this);
        button.setText(ResourceTable.String_md2_fragment_button_walk);
        button.setTextSize(50);
        button.setTextAlignment(TextAlignment.HORIZONTAL_CENTER);
        button.setHeight(80);
        button.setTextColor(Color.BLACK);
        shapeElement.setRgbColor(RgbColor.fromArgbInt(0xFFADADAD));
        button.setBackground(shapeElement);
        button.setClickable(true);
        button.setWidth(BUTTON_WIDTH);
        button.setMarginLeft(BUTTON_MARGIN_LEFT);
        mDirectionalLayout.addComponent(button);

        button = new Button(getContext());
        button.setId(ResourceTable.Integer_md2_fragment_button_wave);
        button.setClickedListener(this);
        button.setText(ResourceTable.String_md2_fragment_button_wave);
        button.setTextSize(50);
        button.setTextAlignment(TextAlignment.HORIZONTAL_CENTER);
        button.setHeight(80);
        button.setTextColor(Color.BLACK);
        shapeElement.setRgbColor(RgbColor.fromArgbInt(0xFFADADAD));
        button.setBackground(shapeElement);
        button.setClickable(true);
        button.setWidth(BUTTON_WIDTH);
        button.setMarginLeft(BUTTON_MARGIN_LEFT);
        mDirectionalLayout.addComponent(button);

        button = new Button(getContext());
        button.setId(ResourceTable.Integer_md2_fragment_button_loop_all);
        button.setClickedListener(this);
        button.setText(ResourceTable.String_md2_fragment_button_loop_all);
        button.setTextSize(50);
        button.setTextAlignment(TextAlignment.HORIZONTAL_CENTER);
        button.setHeight(80);
        button.setTextColor(Color.BLACK);
        shapeElement.setRgbColor(RgbColor.fromArgbInt(0xFFADADAD));
        button.setBackground(shapeElement);
        button.setClickable(true);
        button.setWidth(BUTTON_WIDTH + 20);
        button.setMarginLeft(BUTTON_MARGIN_LEFT);
        mDirectionalLayout.addComponent(button);
        mLayout.addComponent(mDirectionalLayout);
        mLayout.moveChildToFront(mDirectionalLayout);
        mRenderSurface.setTransparent(false);
        WindowManager.getInstance().getTopWindow().get().setTransparent(true);

        return mLayout;
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Integer_md2_fragment_button_salute:
                ((MD2Renderer) mRenderer).playAnimation("salute");
                break;
            case ResourceTable.Integer_md2_fragment_button_death:
                ((MD2Renderer) mRenderer).playAnimation("death1");
                break;
            case ResourceTable.Integer_md2_fragment_button_walk:
                ((MD2Renderer) mRenderer).playAnimation("crwalk");
                break;
            case ResourceTable.Integer_md2_fragment_button_wave:
                ((MD2Renderer) mRenderer).playAnimation("wave");
                break;
            case ResourceTable.Integer_md2_fragment_button_loop_all:
                ((MD2Renderer) mRenderer).playAnimation("loop all");
                break;
        }
    }

    @Override
    public AExampleRenderer createRenderer() {
        return new MD2Renderer(getContext(), this);
    }

    private final class MD2Renderer extends AExampleRenderer {
        private DirectionalLight mLight;
        private VertexAnimationObject3D mOgre;

        public MD2Renderer(Context context, AExampleFragment fragment) {
            super(context, fragment);
        }

        public void playAnimation(String name) {
            if (name.equals("loop all")) {
                mOgre.play();
            } else {
                mOgre.play(name, false);
            }
        }

        @Override
        protected void initScene() {
            mLight = new DirectionalLight();
            mLight.setPower(1);
            mLight.setLookAt(0, 0, 0);
            mLight.enableLookAt();
            getCurrentScene().addLight(mLight);
            getCurrentCamera().setPosition(0, 0, 8);

            ResourceManager resManager = mContext.getResourceManager();
            RawFileEntry rawFileEntry = resManager.getRawFileEntry("resources/rawfile/ogro.md2");
            LoaderMD2 parser = new LoaderMD2(mContext.getResourceManager(),
                    mTextureManager, rawFileEntry);
            try {
                parser.parse(mContext);

                mOgre = (VertexAnimationObject3D) parser
                        .getParsedAnimationObject();
                //mOgre.getMaterial().enableLighting(false);
                mOgre.setScale(.07f);
                mOgre.setY(-1);

                getCurrentScene().addChild(mOgre);

                mOgre.play();
            } catch (ParsingException e) {
                e.printStackTrace();
            }
        }

    }

}
